import Mock from "mockjs";

// 供应商列表
const supplierList = [
  { value: "SUP001", label: "江苏科技有限公司" },
  { value: "SUP002", label: "上海精密制造厂" },
  { value: "SUP003", label: "深圳电子科技有限公司" },
  { value: "SUP004", label: "东莞五金制品厂" },
  { value: "SUP005", label: "无锡机械配件有限公司" },
];

// 生成检验单列表数据
const generateInspectionList = () => {
  return Mock.mock({
    "list|10-20": [
      {
        inspectionNo: /IQC\d{8}\d{4}/,
        materialCode: /MC\d{6}/,
        materialName: "@ctitle(4,8)",
        supplier: function () {
          const supplier =
            supplierList[Math.floor(Math.random() * supplierList.length)];
          return supplier.label;
        },
        batchNo: /BT\d{8}/,
        receiptNo: /RN\d{8}/,
        "totalQuantity|100-1000": 100,
        "sampleQuantity|5-20": 5,
        "status|1": ["pending", "processing", "qualified", "unqualified"],
        inspector: "@cname",
        inspectionTime: "@datetime",
        standard: function () {
          return `标准${Mock.Random.string("upper", 2)}${Mock.Random.string(
            "number",
            4
          )}`;
        },
        // 检验项目
        "items|3-6": [
          {
            itemName: function () {
              const items = [
                "外观",
                "尺寸",
                "硬度",
                "强度",
                "平整度",
                "光洁度",
                "重量",
                "厚度",
              ];
              return items[Math.floor(Math.random() * items.length)];
            },
            standard: function () {
              return `${Mock.Random.float(0, 100, 2, 2)}±${Mock.Random.float(
                0,
                1,
                2,
                2
              )}`;
            },
            result: "@float(0, 100, 2, 2)",
            "isQualified|1": true,
            remark: "@csentence(5, 10)",
          },
        ],
        remark: "@cparagraph(1, 2)",
      },
    ],
    total: function () {
      return this.list.length;
    },
  });
};

// 生成检验模板数据
const generateTemplateList = () => {
  return Mock.mock({
    "list|5-10": [
      {
        templateId: /TL\d{6}/,
        templateName: "@ctitle(4,8)检验标准",
        materialType: function () {
          const types = ["原材料", "外购件", "半成品", "电子元器件"];
          return types[Math.floor(Math.random() * types.length)];
        },
        "items|3-6": [
          {
            itemName: function () {
              const items = [
                "外观",
                "尺寸",
                "硬度",
                "强度",
                "平整度",
                "光洁度",
                "重量",
                "厚度",
              ];
              return items[Math.floor(Math.random() * items.length)];
            },
            standard: function () {
              return `${Mock.Random.float(0, 100, 2, 2)}±${Mock.Random.float(
                0,
                1,
                2,
                2
              )}`;
            },
            method: function () {
              const methods = [
                "目测",
                "卡尺测量",
                "硬度计",
                "拉力测试",
                "千分尺测量",
              ];
              return methods[Math.floor(Math.random() * methods.length)];
            },
            "required|1": true,
          },
        ],
        createTime: "@datetime",
        updateTime: "@datetime",
        creator: "@cname",
      },
    ],
  });
};

// Mock接口
Mock.mock(/\/api\/quality\/inspection\/list/, "get", (options) => {
  const { pageSize = 10, currentPage = 1 } = JSON.parse(options.body || "{}");
  const data = generateInspectionList();
  return {
    code: 200,
    message: "success",
    data: {
      list: data.list.slice(
        (currentPage - 1) * pageSize,
        currentPage * pageSize
      ),
      total: data.total,
    },
  };
});

Mock.mock(/\/api\/quality\/inspection\/template\/list/, "get", () => {
  return {
    code: 200,
    message: "success",
    data: generateTemplateList(),
  };
});

Mock.mock(/\/api\/quality\/inspection\/supplier\/list/, "get", () => {
  return {
    code: 200,
    message: "success",
    data: supplierList,
  };
});

// 新建检验单
Mock.mock(/\/api\/quality\/inspection\/create/, "post", (options) => {
  const data = JSON.parse(options.body);
  return {
    code: 200,
    message: "success",
    data: {
      inspectionNo: `IQC${Mock.Random.date("yyyyMMdd")}${Mock.Random.string(
        "number",
        4
      )}`,
    },
  };
});

// 提交检验结果
Mock.mock(/\/api\/quality\/inspection\/submit/, "post", () => {
  return {
    code: 200,
    message: "success",
  };
});

// 导出数据
Mock.mock(/\/api\/quality\/inspection\/export/, "post", () => {
  return {
    code: 200,
    message: "success",
    data: {
      url: "https://example.com/export/file.xlsx",
    },
  };
});

export default {
  supplierList,
  generateInspectionList,
  generateTemplateList,
};
